package io.github.wjehovah.yyn.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.activity.WebShowActivity;
import io.github.wjehovah.yyn.bean.SubscriptItemBean;
import io.github.wjehovah.yyn.bean.SubscriptItemBean.SubscriptItemDetail;

/**
 * 订阅列表的适配器
 * Created by yingchen on 2015/8/10.
 */
public class SubscriptDetailAdapter extends BaseAdapter implements View.OnClickListener{
    private Context context;
    private List<SubscriptItemDetail> data;
    private LayoutInflater mLayoutInflater;
    private BitmapUtils bitmapUtils;
    public SubscriptDetailAdapter(Context context,List<SubscriptItemBean.SubscriptItemDetail> data){
        this.data = data;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        bitmapUtils = new BitmapUtils(context,"",0.3f,1024*1024*6);
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        int transType = -1;
        String type = data.get(position).type;
        switch (Integer.parseInt(type)){
            case 10: //图片
                transType = 0;
                break;

            case 29: //段子
                transType = 1;
                break;

            case 41: //视频
                transType = 2;
                break;

            case 51: //显示网页
                transType = 3;
                break;
        }
        return transType;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = this.getItemViewType(position);
        ViewHolder holder = new ViewHolder();
        if(convertView == null){
            switch (type){
                case  0 ://图片
                    convertView = mLayoutInflater.inflate(R.layout.photo_item,null);

                    holder.ivUserIcon = (ImageView) convertView.findViewById(R.id.iv_top_item_usericon);
                    holder.tvUserName = (TextView) convertView.findViewById(R.id.tv_top_item_username);
                    holder.tvPubTime = (TextView) convertView.findViewById(R.id.tv_top_item_passtime);

                    holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content_item_detail);
                    holder.ivShowImage = (ImageView) convertView.findViewById(R.id.iv_content_item_picture);

                    holder.tvLike = (TextView) convertView.findViewById(R.id.tv_foot_love);
                    holder.tvHate = (TextView) convertView.findViewById(R.id.tv_foot_hate);
                    holder.tvForward = (TextView) convertView.findViewById(R.id.tv_foot_transmit);
                    holder.tvCommnet = (TextView) convertView.findViewById(R.id.tv_foot_commnet);

                    break;

                case  1: //段子
                    convertView = mLayoutInflater.inflate(R.layout.duanzi_item,null);

                    holder.ivUserIcon = (ImageView) convertView.findViewById(R.id.iv_top_item_usericon);
                    holder.tvUserName = (TextView) convertView.findViewById(R.id.tv_top_item_username);
                    holder.tvPubTime = (TextView) convertView.findViewById(R.id.tv_top_item_passtime);

                    holder.tvContent = (TextView) convertView.findViewById(R.id.tv_essenece_duanzi_content);

                    holder.tvLike = (TextView) convertView.findViewById(R.id.tv_foot_love);
                    holder.tvHate = (TextView) convertView.findViewById(R.id.tv_foot_hate);
                    holder.tvForward = (TextView) convertView.findViewById(R.id.tv_foot_transmit);
                    holder.tvCommnet = (TextView) convertView.findViewById(R.id.tv_foot_commnet);

                    break;

                case  2://视频
                    convertView = mLayoutInflater.inflate(R.layout.video_item,null);

                    holder.ivUserIcon = (ImageView) convertView.findViewById(R.id.iv_top_item_usericon);
                    holder.tvUserName = (TextView) convertView.findViewById(R.id.tv_top_item_username);
                    holder.tvPubTime = (TextView) convertView.findViewById(R.id.tv_top_item_passtime);

                    holder.tvContent = (TextView) convertView.findViewById(R.id.tv_essenece_duanzi_content);
                    holder.flVideoContainer = (FrameLayout) convertView.findViewById(R.id.fl_content_item_video_picture);

                    holder.tvLike = (TextView) convertView.findViewById(R.id.tv_foot_love);
                    holder.tvHate = (TextView) convertView.findViewById(R.id.tv_foot_hate);
                    holder.tvForward = (TextView) convertView.findViewById(R.id.tv_foot_transmit);
                    holder.tvCommnet = (TextView) convertView.findViewById(R.id.tv_foot_commnet);

                    break;

                case  3: //要显示网页的item布局
                    convertView = mLayoutInflater.inflate(R.layout.subscript_detail_item,null);

                    holder.ivUserIcon = (ImageView) convertView.findViewById(R.id.iv_top_item_usericon);
                    holder.tvUserName = (TextView) convertView.findViewById(R.id.tv_top_item_username);
                    holder.tvPubTime = (TextView) convertView.findViewById(R.id.tv_top_item_passtime);

                    holder.tvContent = (TextView) convertView.findViewById(R.id.tv_item_subscript_detail_content);
                    holder.ivShowImage = (ImageView) convertView.findViewById(R.id.iv_item_subscript_detail_show);
                    holder.tvAddAll = (TextView) convertView.findViewById(R.id.tv_item_subscript_detail_loadweb);

                    holder.tvLike = (TextView) convertView.findViewById(R.id.tv_foot_love);
                    holder.tvHate = (TextView) convertView.findViewById(R.id.tv_foot_hate);
                    holder.tvForward = (TextView) convertView.findViewById(R.id.tv_foot_transmit);
                    holder.tvCommnet = (TextView) convertView.findViewById(R.id.tv_foot_commnet);

                    break;
            }

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        //获取对应位置的数据
        SubscriptItemDetail subscriptItemDetail = data.get(position);

        //用户头像
        bitmapUtils.display(holder.ivUserIcon, subscriptItemDetail.profile_image);
        //用户名称
        holder.tvUserName.setText(subscriptItemDetail.name);
        //发表时间
        holder.tvPubTime.setText(subscriptItemDetail.create_time);



        //顶
        holder.tvLike.setText(subscriptItemDetail.love);
        holder.tvHate.setText(subscriptItemDetail.hate);
        holder.tvForward.setText(subscriptItemDetail.repost);
        holder.tvCommnet.setText(subscriptItemDetail.comment);


        switch (type){
            case 0: //图片

                //文字内容
                holder.tvContent.setText(subscriptItemDetail.content);

                //图片
                bitmapUtils.display(holder.ivShowImage,subscriptItemDetail.image);
                break;

            case 1: //段子
                //文字内容
                holder.tvContent.setText(subscriptItemDetail.content);
                break;

            case 2://视频
                TextView tvVideoUrl = new TextView(context);
                tvVideoUrl.setText(subscriptItemDetail.videouri);
                holder.flVideoContainer.addView(tvVideoUrl);
                break;

            case 3://加载网页

                //内容
                holder.tvContent.setText(subscriptItemDetail.richtxt.title);

                //图片
                bitmapUtils.display(holder.ivShowImage,subscriptItemDetail.richtxt.img_url);

                //查看全文
                holder.tvAddAll.setTag(position);
                holder.tvAddAll.setOnClickListener(this);

                break;

        }

        return convertView;
    }



    static class ViewHolder{
        public ImageView ivUserIcon; //用户头像
        public TextView tvUserName;  //用户名称
        public TextView tvPubTime;   //发表时间

        public TextView tvContent;   //文字内容

        public ImageView ivShowImage; //显示图片

        public TextView tvAddAll; //显示全部---加载网页

        public FrameLayout flVideoContainer;

        public TextView tvLike;//顶
        public TextView tvHate;//踩
        public TextView tvForward;//转发
        public TextView tvCommnet;//评论

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_item_subscript_detail_loadweb:
                int position = (int) v.getTag();
                //Toast.makeText(context,"position=="+position,Toast.LENGTH_SHORT).show();
                SubscriptItemDetail subscriptItemDetail = data.get(position);

                //网页地址
                String sourceUrl = subscriptItemDetail.richtxt.source_url;
                //评论数量
                String comment =subscriptItemDetail.comment;

                Intent intent  = new Intent(context, WebShowActivity.class);
                intent.putExtra("COMMENT",comment);
                intent.putExtra("WEBURL",sourceUrl);

                context.startActivity(intent);
                break;

            default:
                break;

        }
    }
}
